//
//  YD_purchaseView.m
//  SpeedDating
//
//  Created by isu on 16/12/16.
//  Copyright © 2016年 isu. All rights reserved.
//

#import "YD_purchaseView.h"
#import "YD_payCell.h"

typedef enum{
    YD_weixinPay,
    YD_zhifibaoPay,
    YD_payClick
}pay;

@interface YD_purchaseView()<UITableViewDelegate,UITableViewDataSource>
/** purchaseBtn*/
@property (nonatomic, strong)UIButton * purchaseBtn;
/** tableView*/
@property (nonatomic, strong)UITableView *tableView;
/** weixinPayBtn*/
@property (nonatomic, strong)UIButton *weixinPayBtn;
/** zhifubaoPay*/
@property (nonatomic, strong)UIButton *zhifubaoPayBtn;
/** money*/
@property (nonatomic, strong)NSString *money;//钻石单位
@property (nonatomic, strong)NSString *payMoney;
@end

@implementation YD_purchaseView
static UIWindow *window_;
+(void)showWithDelegate:(UIViewController *)vc witnMoney:(NSString *)money PayMoney:(NSString *)payMoney{
    // 创建窗口
    window_ = [[UIWindow alloc] init];
    window_.frame = CGRectMake(0, KScreenHeight, KScreenWidth, KScreenHeight);
    window_.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
    window_.hidden = NO;
    
    // 添加发布界面
    YD_purchaseView *purchaseView = [[YD_purchaseView alloc]initWithFrame:CGRectZero witnMoney:money PayMoner:payMoney];
    purchaseView.frame = window_.bounds;
    purchaseView.delegate =(id)vc;
    [window_ addSubview:purchaseView];
    [UIView animateWithDuration:0.3 animations:^{
      window_.frame = [UIScreen mainScreen].bounds;
      purchaseView.frame = window_.bounds;
    }];
}
+(void)Hide{
    window_.hidden = YES;
}
-(instancetype)initWithFrame:(CGRect)frame witnMoney:(NSString *)money PayMoner:(NSString *)payMoney{
    if(self= [super initWithFrame:frame]){
        self.money = money;
        self.payMoney = payMoney;
        self.purchaseBtn= [UIButton new];
        self.purchaseBtn.tag =YD_payClick;
        [self.purchaseBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview: self.purchaseBtn];
        [self.purchaseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.mas_equalTo(self);
            make.height.mas_equalTo(60);
        }];
        [self.purchaseBtn setBackgroundColor:[UIColor redColor]];
        [self.purchaseBtn setTitle:[NSString stringWithFormat:@"确定支付￥%@",self.payMoney] forState:UIControlStateNormal];
        [self.purchaseBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        self.tableView = [UITableView new];
        [self addSubview:self.tableView];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self);
            make.height.mas_equalTo(180);
            make.bottom.mas_equalTo(self.purchaseBtn.mas_top);
        }];
        self.tableView.delegate =self;
        self.tableView.dataSource = self;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tableView.tableFooterView =[UIView new];
        self.tableView.scrollEnabled = NO;
    }
    return self;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 60;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [UIView new];
    UILabel *lab = [UILabel new];
    [view addSubview:lab];
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.centerX.mas_equalTo(view);
    }];
    NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"购买%@钻石",self.money]];
    NSMutableAttributedString *str2 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"支付¥%@",self.payMoney]];
    
    [str1 addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0,str1.length)];
    [str2 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,str2.length)];
    [str1 appendAttributedString:str2];
    lab.attributedText =str1;
    UIView *lineV= [UIView new];
    [view addSubview:lineV];
    [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(view);
        make.height.mas_equalTo(0.8);
    }];
    lineV.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
    return  view;
    
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YD_payCell *cell = [YD_payCell cellWithTableView:tableView];
    if(indexPath.row==0){
        [cell.payBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        cell.payBtn.tag =YD_weixinPay;
        self.weixinPayBtn =cell.payBtn;
    }
    if(indexPath.row==1){
        cell.imageV.image =[UIImage imageNamed:@"zhifubao"];
        cell.payWayLab.text =@"支付宝支付";
        cell.noticeLab.text =@"推荐有支付宝账户的用户使用";
        cell.payBtn.selected =YES;
        [cell.payBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        cell.payBtn.tag =YD_zhifibaoPay;
        self.zhifubaoPayBtn = cell.payBtn;
    }
    return cell;
}
-(void)btnClick:(UIButton*)sender{
    if([sender isEqual:self.weixinPayBtn]){
        sender.selected = YES;
        self.zhifubaoPayBtn.selected = NO;
    }
    else{
        sender.selected = YES;
        self.weixinPayBtn.selected = NO;
    }
    [self.delegate didClickBtn:sender];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
   // NSLog(@"touch = %@",touches);
   CGPoint p = [touches.anyObject locationInView:self];
    p = [self.tableView.layer convertPoint:p fromLayer:self.layer];
    if(![self.tableView.layer containsPoint:p]){
       [YD_purchaseView Hide];
    }
}
@end
